﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace XYZshell.Game.CardGameEngine.Special
{
	/// <summary>
	/// 扑克牌,不能直接构造,只能从扑克牌堆获得
	/// </summary>
	public class PokerCard : Card
	{

		internal PokerCard( PokerCardType cardType, int value )
		{

			Properties["Name"] = GetShortName( cardType, value );
		}

		internal PokerCard( Card card )
		{
			Properties = card.Properties;
			Affect = card.Affect;
		}

		/// <summary>
		/// 扑克花色
		/// </summary>
		public PokerCardType CardType
		{
			get
			{

				return ParseCardType();
			}
		}

		/// <summary>
		/// 扑克的面值
		/// </summary>
		public int Value
		{
			get
			{
				return ParseCardValue();
			}

		}
		/// <summary>
		/// 简短名称
		/// </summary>
		public new string Name
		{
			get
			{
				return Properties["Name"];

			}
		}

		private int ParseCardValue()
		{
			string sType=Name[0].ToString();
			string sVal=Name.Substring( 1 );
			switch (sVal)
			{
				case "A":
					return 1;
				case "J":
					return 11;
				case "Q":
					return 12;
				case "K":
					return 13;
			}
			if (sType == "J")
			{
				if (sVal == "1")
					return 14;
				if (sVal == "2")
					return 15;
			}
			return int.Parse( sVal );
		}

		private PokerCardType ParseCardType()
		{
			string sType=Name[0].ToString();
			switch (sType)
			{
				case "S":
					return PokerCardType.Spades;

				case "H":
					return PokerCardType.Hearts;

				case "C":
					return PokerCardType.Cherry;

				case "D":
					return PokerCardType.Diamond;

				case "J":
					{
						string val=Name.Substring( 1 );
						switch (val)
						{
							case "1":
								return PokerCardType.JockerBlack;
							case "2":
								return PokerCardType.JockerRed;
						}

					}
					break;


			}
			throw new Exception( "ParseCardType 出错" );
		}

		private string GetShortName( PokerCardType cardType, int value )
		{
			string prefix="";
			switch (cardType)
			{
				case PokerCardType.Spades:
					prefix = "S";
					break;
				case PokerCardType.Hearts:
					prefix = "H";
					break;
				case PokerCardType.Cherry:
					prefix = "C";
					break;
				case PokerCardType.Diamond:
					prefix = "D";
					break;
				case PokerCardType.JockerBlack:
					prefix = "J1";
					break;
				case PokerCardType.JockerRed:
					prefix = "J2";
					break;
			}
			string val=value.ToString();
			switch (value)
			{
				case 1:
					val = "A";
					break;
				case 11:
					val = "J";
					break;
				case 12:
					val = "Q";
					break;
				case 13:
					val = "K";
					break;
				case 14:
				case 15:
					val = "";
					break;
			}

			return prefix + val;
		}

		/// <summary>
		/// 从xml中生存扑克牌
		/// </summary>
		/// <param name="xml"></param>
		/// <returns></returns>
		public static new PokerCard FromXML( string xml )
		{
			Card c=Card.FromXML( xml );
			return new PokerCard( c );
		}
		/// <summary>
		/// 转换为友好的字符串格式
		/// </summary>
		/// <returns></returns>
		public override string ToString()
		{
			string prefix="";
			switch (CardType)
			{
				case PokerCardType.Spades:
					prefix = "♠";
					break;
				case PokerCardType.Hearts:
					prefix = "♥";
					break;
				case PokerCardType.Cherry:
					prefix = "♣";
					break;
				case PokerCardType.Diamond:
					prefix = "♦";
					break;
				case PokerCardType.JockerBlack:
					prefix = "JockerBlack";
					break;
				case PokerCardType.JockerRed:
					prefix = "JockerRed";
					break;
			}
			string val=this.Value.ToString();
			if (Value == 1)
				val = "A";
			if (Value > 10)
			{
				switch (Value)
				{
					case 1:
						val = "A";
						break;
					case 11:
						val = "J";
						break;
					case 12:
						val = "Q";
						break;
					case 13:
						val = "K";
						break;
					case 14:
					case 15:
						val = "";
						break;

				}
			}
			return val + prefix;
		}
	}
}
